<?php
header('Content-Type:application/json');
include("./API.php");
include("../config.php");
tongji("BaiduKeyword");
require_once 'data/BaiduKeyword.php';
$wd = $_REQUEST['msg'];//要查询的关键词
$type = $_REQUEST['type'];//返回格式，默认json可选text
$hh = $_REQUEST["hh"]?:"\n";//换行符号(默认\n)
$data = json_decode(file_get_contents('https://www.baidu.com/sugrec?ie=utf-8&json=1&prod=pc&wd=' . $wd), true);
if ($wd == null) {
    switch ($type) {
        case 'text':
            exit("".$web["ming"]."API百度搜索关键词".$hh."━━━━━━━━━".$hh."关键词空白".$hh."━━━━━━━━━".$hh."Tips:".$web["ming"]."API技术支持");
        default:
            exit(send(array('code' => -1, 'text' => '关键词空白')));
    }
}
if (!$data['g']) {
    switch ($type) {
        case 'text':
            exit("".$web["ming"]."API百度搜索关键词".$hh."━━━━━━━━━".$hh."关键词过长或含有特殊字符".$hh."━━━━━━━━━".$hh."Tips:".$web["ming"]."API技术支持");
        default:
            exit(send(array('code' => -2, 'text' => '关键词过长或含有特殊字符')));
    }
}
$num = count($data['g']);
switch ($type) {
    //判断输出类型
    case 'text':
        echo "".$web["ming"]."API百度搜索关键词".$hh."";
        echo "━━━━━━━━━".$hh."";
        for ($i = 0; $i < $num; $i++) {
            echo "".$i + 1.0 . ":".$data['g'][$i]['q']."".$hh."";
        }
        echo "━━━━━━━━━".$hh."";
        echo "Tips:".$web["ming"]."API技术支持";
        exit;
    default:
        foreach ($data['g'] as $v) {
            $array[] = array('msg' => $v['q']);
        }
        exit(send(array('code' => 1, 'text' => '获取成功', 'total' => $num, 'data' => $array)));
}